# -*- coding: utf-8 -*-
import subprocess

from flask import json
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops

from facility.reg_fn import get_number


class FujiangSpider(RedisSpider):
    name = 'fujiang'
    redis_key = 'fujiang:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    con.lpush(redis_key, v8host)

    def parse(self, response):
        for i in range(37):
            yield Request(
                url='https://www.fujiangwealth.com/lend/ajax_get_fjd2.aspx?dqys={}&type=+&jkqq=+&fbzt=+&xmsy=+&jefw=+'.format(
                    i + 1),
                dont_filter=True,
                callback=self.parse_end)

    def parse_end(self, response):
        q = Ops(response)
        for i in range(q.get_num('/html/body/table/tr') - 1):
            a = i + 1
            idx = q.get_attr("/html/body/table/tr[{}]/td[7]/a".format(a), 'href').split("/")[-1]
            yield Request(
                url="https://www.fujiangwealth.com/{}".format(idx),
                dont_filter=True,
                callback=self.parse_detail_end)

    #
    #
    # def parse_end(self,response):
    #     q = Ops(response)
    #     for i in range(q.get_num('/html/body/table/tr')-1):
    #         a = i +1
    #         name = q.get_str('/html/body/table/tr[{}]/td'.format(a))
    #         amount = q.get_str('/html/body/table/tr[{}]/td[4]/b'.format(a))
    #         rate = q.get_str('/html/body/table/tr[{}]/td[5]/b'.format(a))
    #         cycle = q.get_str('/html/body/table/tr[{}]/td[1]/b'.format(a)).replace('融资期限：','').replace('个月','').strip()
    #         time = q.get_str('/html/body/table/tr[{}]/td[2]/b'.format(a))
    #         data = name+' '+amount+' '+rate+' '+cycle+' '+time
    #         subprocess.call("echo  {} >>新华富疆.txt".format(data), shell=True)

    def parse_detail_end(self, response):
        q = Ops(response)

        name = q.get_str("/html/body/div[4]/div/div[1]/h4/b")
        amount = str(q.get_str("/html/body/div[4]/div/div[1]/u/b[2]/strong"))

        rate = str(q.get_str("/html/body/div[4]/div/div[1]/u/b[1]/strong")) + "%"
        start = q.get_str("/html/body/div[4]/div/div[1]/b/strong[1]").split(" ")[0].replace("/", "-")
        cycle = str(get_number(response.xpath("/html/body/div[4]/div/div[1]/strong").extract()[0].strip())[-1])
        way = q.get_str("/html/body/div[4]/div/div[1]/b/strong[4]")
        data = name + " " + amount + " " + rate + " " + start + " " + cycle + " " + way

        subprocess.call("echo  '{}' >>富疆.txt".format(data), shell=True)
